package com.tianqu.tms.www.rental.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;

/**
 * 租位查询/修改 - 查询输入 action
 * @author mufeiyu
 *
 */
public class RentalLocationSearchInputAction extends TmsAction {

	
	private Logger logger = Logger.getLogger(RentalLocationSearchInputAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		//去掉缓存
		if(uc.getAttribute("seniorForm")!=null)
		{
			uc.removeAttribute("seniorForm");
		}
		
		Codes codes = Codes.getInstance();
		RentalLocationViewHelper viewHelper = new RentalLocationViewHelper();
		
		String searchRentalLocationButton = request.getParameter("searchRentalLocationButton");
		
		// 初次
		if (StringUtil.isNullOrBlank(searchRentalLocationButton)) {
			
			// 租位编号
			viewHelper.setLocationSerial("");
			
			// 租位类型
			List rentalTypeList = codes.getCodeslistByItemId(Codes.RENTAL_TYPE);

			for (Object object : rentalTypeList) {
				String checked = "checked";
				HashMap codesMap = (HashMap)object;
				codesMap.put("checked", checked);
			}
			viewHelper.setRentalTypeList(rentalTypeList);
			
			// 租位状态
			List rentalStatusList = getRentalStatusList();
			viewHelper.setRentalStatusList(rentalStatusList);
			
			// 租位面积(㎡) / 区间		
			viewHelper.setAcreageS("");
			viewHelper.setAcreageE("");
			
			// 车位数量(个) / 区间
			viewHelper.setCarPortS("");
			viewHelper.setCarPortE("");
			
			// 租位区域
			List areaList = codes.getCodeslistByItemId(Codes.RENTAL_AREA);
			viewHelper.setRentalAreaList(areaList);
			
			// 登记日期 / 区间
			viewHelper.setUpdateTimeS("");
			viewHelper.setUpdateTimeE("");
		} else {
			// Controller验证失败
			if("true".equals(uc.getAttribute("errFlg"))){
				viewHelper = (RentalLocationViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
				
				uc.removeAttribute(TmsConstants.VIEWHELP);
				uc.removeAttribute("errFlg");
				uc.setAttribute("searchFlg", "1");
			} else if (!StringUtil.isNullOrBlank(searchRentalLocationButton) && "1".equals(searchRentalLocationButton)){
				// validation-rental.xml验证失败				
				RentalLocationAddInputForm rentalForm = (RentalLocationAddInputForm) uc.getInputForm();
				
				// 租位编号
				viewHelper.setLocationSerial(rentalForm.getLocationSerial());
				
				// 租位类型
				viewHelper.setRentalTypeList(convertRentalType(rentalForm.getStrRentalType()));
				
				// 租位状态
				viewHelper.setRentalStatusList(convertRentalStatus(rentalForm.getRentalStatus()));
				
				// 租位面积(㎡) / 区间		
				viewHelper.setAcreageS(rentalForm.getAcreageS());
				viewHelper.setAcreageE(rentalForm.getAcreageE());
				
				// 车位数量(个) / 区间
				viewHelper.setCarPortS(rentalForm.getCarPortS());
				viewHelper.setCarPortE(rentalForm.getCarPortE());
				
				// 租位区域
				List areaList = codes.getCodeslistByItemId(Codes.RENTAL_AREA);
				viewHelper.setRentalAreaList(areaList);
				viewHelper.setRentalArea(rentalForm.getRentalArea());
				
				// 登记日期 / 区间
				viewHelper.setUpdateTimeS(rentalForm.getUpdateTimeS());
				viewHelper.setUpdateTimeE(rentalForm.getUpdateTimeE());
			}
		}

		uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);

		uc.setAttribute("searchFlg", "1");
		request.setAttribute(TmsConstants.VIEWHELP, viewHelper);		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	private  List getRentalStatusList() {
		Map map = null;// 用于存储itemCode和itemValue
		List list = new ArrayList();
		
		map = new HashMap();
		map.put("itemCode", 1);
		map.put("itemValue", "已出租");
		map.put("checked", "checked");
		list.add(map);
		
		map = new HashMap();
		map.put("itemCode", 2);
		map.put("itemValue", "未出租");
		map.put("checked", "checked");
		list.add(map);
		
		if (list.size()==0) {
			return null;
		}		

		return list;
	}

	private List convertRentalStatus(String[] rentalStatus) throws TmsDaoException{
		Map map = null;// 用于存储itemCode和itemValue
		List list = new ArrayList();
		
		map = new HashMap();
		map.put("itemCode", 1);
		map.put("itemValue", "已出租");
		if (null != rentalStatus && rentalStatus.length > 0) {
			for (int i = 0; i < rentalStatus.length; i++){
				if ("1".equals(rentalStatus[i])) map.put("checked", "checked");
			}
		}
		list.add(map);
		
		map = new HashMap();
		map.put("itemCode", 2);
		map.put("itemValue", "未出租");
		if (null != rentalStatus && rentalStatus.length > 0) {
			for (int i = 0; i < rentalStatus.length; i++){
				if ("2".equals(rentalStatus[i])) map.put("checked", "checked");
			}
		}
		list.add(map);
		
		if (list.size()==0) {
			return null;
		}		

		return list;
	}
	private List convertRentalType(String[] rentalType) throws TmsDaoException{
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		if(null != rentalType && rentalType.length > 0){
			for (int i = 0; i < rentalType.length; i++){
				int itemCode = StringUtils.isNumeric(rentalType[i])?Integer.parseInt(rentalType[i]):-1;
				map.put(itemCode, itemCode);
			}
		}
		List codeslist = Codes.getInstance().getCodeslistByItemId(Codes.RENTAL_TYPE);
		for (Object object : codeslist) {
			String checked = "";
			HashMap codesMap = (HashMap)object;
			if(map.get(codesMap.get("itemCode"))!=null ) checked = "checked";
			codesMap.put("checked", checked);
		}
		return codeslist;
	}
}